package com.molyfun.parents.activity.signup;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.bean.SignUpNoticeResponse;
import com.molyfun.parents.utils.GsonUtil;
import com.molyfun.parents.utils.PreferencesStorageUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignUpStartActivity extends BaseActivity implements View.OnClickListener {

    private String TAG = "SignUpStartActivity";
    @InjectView(R.id.base_title_left)
    ImageView mCloseButton;
    @InjectView(R.id.base_title)
    TextView mTitle;
    @InjectView(R.id.content)
    TextView mContent;
    @InjectView(R.id.submit_button)
    TextView mSubmitButton;
    @InjectView(R.id.layout_relative_layout)
    RelativeLayout mLayout_relative_layout;
    @InjectView(R.id.empty_layout)
    LinearLayout mEmpty_layout;
    private String codeValue;
    private String departid = "";
    private SignUpNoticeResponse.SignUpNotice signUpNoticeBean;

    public static void startActivity(Context context, SignUpNoticeResponse.SignUpNotice signUpNoticeBean) {
        Intent intent = new Intent();
        intent.setClass(context, SignUpStartActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("sign_up_notice", signUpNoticeBean);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up_start);
        ButterKnife.inject(this);
        getSupportActionBar().setTitle("");
        mTitle.setText("报名须知");

        if(PreferencesStorageUtil.getServerMode(SignUpStartActivity.this) == PreferencesStorageUtil.ServerMode.TEST){
            codeValue = "A0000004";
        }else{
            codeValue = "A0001930";
        }
        signUpNoticeBean = getIntent().getParcelableExtra("sign_up_notice");

        initView();
//        loadData(codeValue);

//                    LogUtils.d(TAG + departid + "content" + content);
        if (signUpNoticeBean != null) {
            departid = signUpNoticeBean.getDepartid();
            String content = signUpNoticeBean.getEnrollrules();
            mLayout_relative_layout.setVisibility(View.VISIBLE);
            mContent.setText(content);
            mEmpty_layout.setVisibility(View.GONE);
        } else {
            loadData(codeValue);
        }

    }


    private void initView() {
        getSupportActionBar().setTitle("");
        mCloseButton.setOnClickListener(this);
        mSubmitButton.setOnClickListener(this);
        mLayout_relative_layout.setVisibility(View.GONE);
    }

    private void loadData(final String codeValue) {
        showProgressDialog();
        Call<SignUpNoticeResponse> call = MFPApplication.getInstance().getRestClient().signUpNotice(codeValue, "continue");
        call.enqueue(new Callback<SignUpNoticeResponse>() {

            @Override
            public void onResponse(Call<SignUpNoticeResponse> call, Response<SignUpNoticeResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null && response.body().getCode() == 200) {
                    signUpNoticeBean = GsonUtil.fromJson(response.body().getData(), SignUpNoticeResponse.SignUpNotice.class);

                    departid = signUpNoticeBean.getDepartid();
                    String content = signUpNoticeBean.getEnrollrules();
//                    LogUtils.d(TAG + departid + "content" + content);
                    if (signUpNoticeBean != null && !TextUtils.isEmpty(content)) {
                        mLayout_relative_layout.setVisibility(View.VISIBLE);
                        mContent.setText(content);
                        mEmpty_layout.setVisibility(View.GONE);
                    } else {
                        mContent.setText("暂无内容");
                        mLayout_relative_layout.setVisibility(View.VISIBLE);
                        mEmpty_layout.setVisibility(View.GONE);
                    }


                } else {
                    toast(response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<SignUpNoticeResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("报名失败");
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.submit_button:
                if (signUpNoticeBean != null) {
//                    SignUpBasicInfoActivity.startActivity(this, departid);
                    SignUpBasicInfoActivity.startActivity(this, signUpNoticeBean);
                }

                break;
        }
    }

}

